package com.example.drss.fragment;

import java.util.ArrayList;
import java.util.List;

import com.example.drss.DRSSActivity;
import com.example.drss.DishesActivity;
import com.example.drss.R;
import com.example.drss.SellerActivity;
import com.example.drss.TasteActivity;
import com.example.drss.adapter.GridViewAdapter;
import com.example.drss.bean.Menu;
import com.example.drss.itemdata.ItemData;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class ThreeFragment extends Fragment {

	View view;

	ViewPager viewPager;
	List<View> views;
	RadioGroup radioGroup;
	RadioButton radioOne;
	RadioButton radioTwo;
	GridView  gridView;
	EditText queryView;

	DRSSActivity activity;

	List<ItemData> data;

	Cursor cursor;

	Button suanImageButton;
	Button tianImageButton;
	Button laImageButton;
	Button xianImageButton;
	Button qingdanImageButton;
	Button xiaochiImageButton;
	
	OnClickListener listener=new OnClickListener() {
		
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.Button_suan:
				Intent intent=new Intent(activity,TasteActivity.class);
				intent.putExtra("databasePath_taste", activity.databasePath);
				intent.putExtra("taste", "酸");
				startActivity(intent);
				break;
			case R.id.Button_tian:
				Intent intent1=new Intent(activity,TasteActivity.class);
				intent1.putExtra("databasePath_taste", activity.databasePath);
				intent1.putExtra("taste", "甜");
				startActivity(intent1);
				break;
			case R.id.Button_la:
				Intent intent2=new Intent(activity,TasteActivity.class);
				intent2.putExtra("databasePath_taste", activity.databasePath);
				intent2.putExtra("taste", "辣");
				startActivity(intent2);
				break;
			case R.id.Button_xian:
				Intent intent3=new Intent(activity,TasteActivity.class);
				intent3.putExtra("databasePath_taste", activity.databasePath);
				intent3.putExtra("taste", "咸");
				startActivity(intent3);
				break;
			case R.id.Button_qingdan:
				Intent intent4=new Intent(activity,TasteActivity.class);
				intent4.putExtra("databasePath_taste", activity.databasePath);
				intent4.putExtra("taste", "清淡");
				startActivity(intent4);
				break;
			case R.id.Button_qita:
				Intent intent5=new Intent(activity,TasteActivity.class);
				intent5.putExtra("databasePath_taste", activity.databasePath);
				intent5.putExtra("taste", "小吃");
				startActivity(intent5);
				break;

			default:
				break;
			}
		}
	};

	public static class MyPagerAdapter extends PagerAdapter{


		List<View> views;
		public MyPagerAdapter(List<View> views){
			this.views=views;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return views==null?null:views.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0==arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub

			ViewPager vp=(ViewPager)container;
			View v=views.get(position);
			vp.addView(v);
			return v;
			//return super.instantiateItem(container, position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			ViewPager vp=(ViewPager)container;
			View v=views.get(position);
			vp.removeView(v);
			//super.destroyItem(container, position, object);
		}
	}

	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		view=inflater.inflate(R.layout.fragment_three, container, false);
		activity=(DRSSActivity)getActivity();
		viewPager=(ViewPager)view.findViewById(R.id.viewpage_dishes);
		views=new ArrayList<View>();
		
		LayoutInflater inflater1=LayoutInflater.from(activity);
		View pageOne=inflater1.inflate(R.layout.viewpager_dishes_one, null);
		View pageTwo=inflater1.inflate(R.layout.viewpager_dishes_two, null);

		views.add(pageOne);
		views.add(pageTwo);

		radioGroup = (RadioGroup)view.findViewById(R.id.radioGroup_viewpager);
		radioOne=(RadioButton)view.findViewById(R.id.radio_page_one);
		radioTwo=(RadioButton)view.findViewById(R.id.radio_page_two);

		//Button button=(Button)pageTwo.findViewById(R.id.button1);
		queryView=(EditText)pageTwo.findViewById(R.id.editText_query_all);
		gridView=(GridView)pageTwo.findViewById(R.id.gridView_all_dishes);

		suanImageButton=(Button)pageOne.findViewById(R.id.Button_suan);
		tianImageButton=(Button)pageOne.findViewById(R.id.Button_tian);
		laImageButton=(Button)pageOne.findViewById(R.id.Button_la);
		xianImageButton=(Button)pageOne.findViewById(R.id.Button_xian);
		qingdanImageButton=(Button)pageOne.findViewById(R.id.Button_qingdan);
		xiaochiImageButton=(Button)pageOne.findViewById(R.id.Button_qita);
		
		suanImageButton.setOnClickListener(listener);
		tianImageButton.setOnClickListener(listener);
		laImageButton.setOnClickListener(listener);
		xianImageButton.setOnClickListener(listener);
		qingdanImageButton.setOnClickListener(listener);
		xiaochiImageButton.setOnClickListener(listener);

		data = new ArrayList<ItemData>();
		cursor=activity.getAll();
		if (cursor!=null) {
			//读取所有纪录
			List<Menu> menus =new ArrayList<Menu>();
			while(cursor.moveToNext()){
				Menu menu=new Menu();
				menu.setImage(cursor.getString(2));
				menu.setName(cursor.getString(1));
				menus.add(menu);
			}
			for(Menu menu:menus){
				ItemData data1= new ItemData();
				data1.setName(menu.getName());
				data1.setImage(menu.getImage());
				System.out.println(data1.getImage());
				data.add(data1);
			}		
		}
		GridViewAdapter gridViewAdapter=new GridViewAdapter(activity, data);
		gridView.setAdapter(gridViewAdapter);
		
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(activity,DishesActivity.class);
				ItemData itemData=data.get(position);
				intent.putExtra("name", itemData.getName());
				intent.putExtra("databasePath_dishes", activity.databasePath);
				startActivity(intent);
			}
		});

		queryView.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				data = new ArrayList<ItemData>();
				cursor=activity.query(queryView.getText().toString());
				if (cursor!=null) {
					//读取所有纪录
					List<Menu> menus =new ArrayList<Menu>();
					while(cursor.moveToNext()){
						Menu menu=new Menu();
						menu.setName(cursor.getString(1));
						menu.setImage(cursor.getString(2));
						menus.add(menu);
					}
					for(Menu menu:menus){
						ItemData data1= new ItemData();
						data1.setName(menu.getName());
						data1.setImage(menu.getImage());
						data.add(data1);
					}		
				}
				GridViewAdapter gridViewAdapter=new GridViewAdapter(activity, data);
				gridView.setAdapter(gridViewAdapter);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

		MyPagerAdapter myPagerAdapter=new MyPagerAdapter(views);
		viewPager.setAdapter(myPagerAdapter);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {

				switch (arg0) {
				case 0:
					radioOne.setChecked(true);
					break;
				case 1:
					radioTwo.setChecked(true);
					break;

				default:
					break;
				}
				// TODO Auto-generated method stub
				//Toast.makeText(MainActivity.this, "page index"+arg0, Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});


		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.radio_page_one:
					viewPager.setCurrentItem(0);
					break;
				case R.id.radio_page_two:
					viewPager.setCurrentItem(1);
					break;

				default:
					break;
				}
			}
		});
		return view;
	}

}
